<?php

namespace App\Http\Model\RoomBackgroundPicture;

use App\Http\Model\Base;

class BackgroundPictureSpecial extends Base
{
    /**
     * 表名字
     * @var string
     */
    protected $table = 'room_background_picture_special';

    /**
     * 指示模型是否自动维护时间戳
     *
     * @var bool
     */
    public $timestamps = true;

    /**
     * 模型日期列的存储格式。
     *
     * @var string
     */
    protected $dateFormat = 'U';

    /**
     * 可以被批量赋值的属性。
     *
     * @var array
     */
    protected $fillable = ['id', 'management_id', 'room_uid', 'created_at', 'updated_at'];

    /**
     * 构造函数
     * @param array $attributes
     */
    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    /**
     * 获取指定房间uuid串
     *
     * Created at 2021/11/12 11:52
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @param int $managementId
     * @return string
     */
    public static function getRoomUuids(int $managementId): string
    {
        $roomUuids = self::query()->where('management_id', $managementId)->pluck('room_uid')->toArray();
        return $roomUuids ? implode(',', $roomUuids) : '';
    }

    /**
     * 重置
     *
     * Created at 2021/11/12 11:55
     * @Author <yuxiang.zhu@asiainnovations.net>
     * @param int $managementId
     * @param array $roomUuids
     */
    public static function resetRoomUuids(int $managementId, array $roomUuids)
    {
        if (!$managementId) {
            return;
        }

        self::query()->where('management_id', $managementId)->delete();
        $records = [];
        foreach ($roomUuids as $roomUuid) {
            $roomUuid = trim($roomUuid);
            $records[] = ['management_id' => $managementId, 'room_uid' => $roomUuid, 'created_at' => time(), 'updated_at' => time()];
        }

        if ($records) {
            self::query()->insert($records);
        }
    }
}